﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using HigLabo.WindowsAzure.RestService.Extensions;

namespace HigLabo.WindowsAzure.RestService.ServiceManagement
{
    public class HostedService
    {
        public static readonly String ElementName = "HostedService";

        public String Url { get; private set; }
        public String ServiceName { get; private set; }
        public HostedServiceProperties HostedServiceProperties { get; private set; }
        public Collection<Deployment> _Deployments = new Collection<Deployment>();
        public IEnumerable<Deployment> Deployments 
        { 
            get { foreach (var item in _Deployments) { yield return item; } } 
        }
        
        public HostedService(XElement xml)
        {
            XElement el;
            IEnumerable<XElement> els;
            if (xml == null)
            {
                throw new ArgumentNullException("xml");
            }
            this.Url = xml.ValueOrEmpty("Url");
            this.ServiceName = xml.ValueOrEmpty("ServiceName");
            el = xml.ElementByLocalName(HostedServiceProperties.ElementName);
            if (el != null)
            {
                this.HostedServiceProperties = new HostedServiceProperties(el);
            }
            el = xml.ElementByLocalName("Deployments");
            if (el != null)
            {
                els = el.ElementsByLocalName(Deployment.ElementName);
                foreach (var item in els)
                {
                    el = item;
                    var add = new Deployment(el);
                    this._Deployments.Add(add);
                }
            }
        } 
    }
}
